package com.zhl.dao;

import com.zhl.dao.dasedao.BaseDao;
import com.zhl.model.Article;
import com.zhl.utils.JdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class ArticleDao extends BaseDao {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());

    public ArticleDao() {
        super(Article.class);
    }

    public List<Article> findArticles(String title,Integer startIndex,Integer pageSize){
        List<Article> articleList = null;
        if (title == null || title == ""){
            try {
                String sql = "select * from article limit ?,?";
                articleList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Article.class),startIndex,pageSize);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            try {
                String sql = "select * from article where title like '%' ? '%' limit ?,?";
                articleList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Article.class),title,startIndex,pageSize);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return articleList;
    }

    public Integer findAll() {
        String sql = "select count(*) as total from article";
        Map<String, Object> map = jdbcTemplate.queryForMap(sql);
        return Integer.parseInt(map.get("total").toString());
    }

    public Map<String,Object> findArticleById(Integer id){
        Map<String,Object> map = null;
        String sql = "select a.*,(select count(*) from favorite where a_id = ?) as \"count\" " +
                "from article a where a.id = ?";
        try {
            map = jdbcTemplate.queryForMap(sql,id,id);
        }catch (Exception e){
            return null;
        }
        return map;
    }

    public boolean deleteCollect(Integer uId,Integer aId){
        int i = 0;
        String sql = "delete from favorite where u_id = ? and a_id = ?";
        try {
            i = jdbcTemplate.update(sql, uId, aId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i > 0;
    }

    public List<Map<String,Object>> findArticleByUid(Integer uId,String title){
        List<Map<String,Object>> articlesByuId = null;
        if (title == null){
            String sql = "select a.*,(select count(u_id) from favorite where a_id = a.id) as \"count\" " +
                    "from article a " +
                    "where a.id in (select a_id from favorite where u_id = ?)";
            try {
                articlesByuId = jdbcTemplate.queryForList(sql,uId);
            }catch (Exception e){
                return null;
            }
        }else {
            String sql = "select a.*,(select count(u_id) from favorite where a_id = a.id) as \"count\" " +
                    "from article a " +
                    "where a.id in (select a_id from favorite where u_id = ?) and a.title like '%' ? '%'";
            try {
                articlesByuId = jdbcTemplate.queryForList(sql,uId,title);
            }catch (Exception e){
                return null;
            }
        }
        return articlesByuId;
    }
}
